import numpy as np

from implement.layers.basic.affine import Affine
from utils.functions_collect import mean_squared_error, sigmoid

np.random.seed(8)
x = np.random.rand(100, 1)
y = np.sin(2 * np.pi * x) + np.random.rand(100, 1)

l1 = Affine(10)
l2 = Affine(1)


def predict(x):
    y = l1(x)
    y = sigmoid(y)
    y = l2(y)
    return y


lr = 0.2
iters = 10000

for i in range(iters):
    y_pred = predict(x)
    loss = mean_squared_error(y, y_pred)

    l1.cleargrads()
    l2.cleargrads()
    loss.backward()

    for l in [l1, l2]:
        for p in l.params():
            p.data = p.data - lr * p.grad.data

    if i % 1000 == 0:
        print(loss.data)
